//
// Created by LiuYou on 2021/8/3.
//


#include <cstdio>
#include <cstdlib>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <iostream>
#include "Proto.hpp"


int main() {
    int keyProj = static_cast<int>(KEY_PROJ);
    key_t key = ftok( KEY_PATH, keyProj );
    if (key < 0) {
        perror( "ftok()" );
        exit( 1 );
    }

    int msgId = msgget( key, IPC_CREAT | 0600 );
    if (msgId < 0) {
        perror( "msgget()" );
        exit( 1 );
    }

    Msg recBuf;
    while (true) {
        ssize_t ret = msgrcv( msgId, &recBuf, sizeof( recBuf ) - sizeof( long ),
                              0, 0 );
        if (ret < 0) {
            perror( "msgrcv()" );
            exit( 1 );
        }
        fprintf( stdout, "NAME = %s\n", recBuf.name );
        fprintf( stdout, "MATH = %d\n", recBuf.math );
        fprintf( stdout, "CHINESE = %d\n", recBuf.chinese );
        std::cout << "recBuf: {" << recBuf << "}" << std::endl;
    }


    msgctl( msgId, IPC_RMID, nullptr );


    exit( 0 );
}